home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Technotools
/
Technotools (Chestnut CD-ROM)(1993).ISO
/
lang_c
/
optans10
/
demoansi.c
next >
Wrap
C/C++ Source or Header
|
1992-06-14
|
13KB
|
460 lines
#include "optansi.h"
#include "auser.h"
#include "skeys.h"
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
/* Global variables */
// Counters and Timers
unsigned ocnt, nocnt;
clock_t begin, otime, notime;
pl(char *s) {
printf(s);
aupdatexy();
if(getch()==27)
exit(1);
return;
}
int test1() {
#define MEMBERS 10
// X positions
const char x[]={ 1, 2, 78, 78, 1, 1, 39, 39, 1, 1 };
// Y positions
const char y[]={ 1, 2, 2, 24, 24, 2, 12, 13, 14, 1 };
// Attributes
const char a[]={ 2, 7, 15, 31, 14, 13, 63,191, 79, 7 };
int i, j;
// Optimizations Count
acolor(7);
aclrscr();
ocnt=0;
otime=0;
for (j=0; j<100; j++) {
for (i=0; i<MEMBERS; i++) {
begin=clock();
ocnt+=agotoxy(x[i], y[i]); // Move cursor to new position
ocnt+=acolor(a[i]); // Set new color
printf("%X\b", i); // Put hex number
ocnt++; // Add 1 for output of hex character
otime+=clock()-begin;
if (!j) {
gotoxy(65, 1); // conio functions used to avoid interfering with ANSI
printf("X%d Y%d A%d %X", x[i], y[i], a[i], i);
aclreol();
if (getch()==27)
exit(1);
if (i==MEMBERS-1) {
gotoxy(63, 1);
atextattr(7);
printf("Now 100x repeated");
if (getch()==27)
exit(1);
}
gotoxy(x[i], y[i]); // Restore location
}
}
begin=clock();
ocnt+=acolor(7); // Set color for clear screen to black
ocnt+=aclrscr(); // Clear the screen
otime+=clock()-begin;
}
// Non-optimizations Count
atextattr(7);
aclrscr();
gotoxy(63, 1);
pl("Now Nonoptimized");
nocnt=0;
notime=0;
for (j=0; j<100; j++) {
for (i=0; i<MEMBERS; i++) {
begin=clock();
nocnt+=oldgotoxy(x[i], y[i]); // Move cursor to new position
nocnt+=oldcolor(a[i]); // Set new color
printf("%X\b", i); // Put hex number
nocnt++; // Add 1 for output of hex character
notime+=clock()-begin;
}
begin=clock();
nocnt+=oldcolor(7); // Set color for clear screen to black
nocnt+=oldclrscr(); // Clear the screen
notime+=clock()-begin;
}
return;
}
int test2() {
int i, j;
struct point {
char x, y, a;
} *ptr, *st;
if ((st = malloc(100*MEMBERS*sizeof(struct point))) == NULL)
{
printf("Not enough memory for Test 2\n");
return(0);
}
ptr=st;
// Optimizations Count
randomize();
acolor(7);
aclrscr();
ocnt=0;
otime=0;
for (j=0; j<100*MEMBERS; j++) {
ptr->x=random(80)+1;
ptr->y=random(24)+1;
ptr->a=random(256);
begin=clock();
ocnt+=agotoxy(ptr->x, ptr->y); // Move cursor to new position
ocnt+=acolor(ptr->a); // Set new color
ocnt+=printf("%X", j); // Put hex number
aupdatexy();
otime+=clock()-begin;
ptr++;
}
ptr=st;
// Non-optimizations Count
gotoxy(63, 1);
atextattr(7);
pl("Now Nonoptimized");
aclrscr();
nocnt=0;
notime=0;
for (j=0; j<100*MEMBERS; j++) {
begin=clock();
nocnt+=oldgotoxy(ptr->x, ptr->y); // Move cursor to new position
nocnt+=oldcolor(ptr->a); // Set new color
nocnt+=printf("%X", j); // Put hex number
notime+=clock()-begin;
ptr++;
}
free(st);
return(1);
}
test3() {
char s[25];
unsigned i;
/* LOCATION FUNCTIONS */
aclrscr();
pl("LOCATION FUNCTIONS: aclrscr()\n");
pl("aupdatexy() - updates axpos and aypos after a printf\n");
aupdatexy();
pl("aupdatexy1() - same as aupdatexy except that it uses low memory to get values\n");
aupdatexy1();
ahome(); /* MAJOR BUG HERE */
pl("ahome()");
aclreol();
pl("");
pl(" aclreol() - Note how everything disappeared to the right?");
adown(5);
pl("adown(5)");
aleft(25);
pl("aleft(25)");
aup(2);
pl("aup(2)");
aright(7);
pl("aright(7)");
agotoxy(1,12);
pl("agotoxy(1,12)");
pl("\nasavecursor() - Note the cursor location now ->");
asavecursor();
// Note this next line uses oldgotoxy which does not update the global variables axpos and aypos
oldgotoxy(1,24);
pl("gotoxy(1,24) - This goto uses some other goto function that doesn't update axpos and aypos so aforcegotoxy can help to resolve");
aforcegotoxy(1,12);
pl("aforcegotoxy(1,12)");
arestorecursor();
pl("X\narestorecursor() - location is restored to where the X above is placed");
/* COLOR FUNCTIONS */
aclrscr();
pl("COLOR FUNCTIONS:\n");
acolor(ATTR(YELLOW, GREEN));
pl("acolor(ATTR(YELLOW,GREEN))\n");
atextattr(ATTR(WHITE,BLUE));
pl("atextattr(ATTR(WHITE,BLUE)) - same as acolor\n");
atextcolor(LIGHTRED);
pl("atextcolor(LIGHTRED) - foreground color\n");
atextbackground(RED);
pl("atextbackground(RED) - background color\n");
aresetcolor();
pl("aresetcolor()\n");
ahighlight();
pl("ahighlight() or ahighvideo()\n");
ablink();
pl("ablink()\n");
anohighlight();
pl("anohighlight() or alowvideo()\n");
// anormvideo()
anoblink();
pl("anoblink()\n\nThese next 3 functions are special ANSI color commands and behave differently\n on different emulators. See DOCS.\n");
aunderline(;);
pl("aunderline(equation) ");
areverse(;);
pl("areverse(equation) ");
ainvisible(;);
pl("ainvisible(equation)\n");
oldcolor(7);
printf("That last color was ainvisible() in case you could not see it.\n\n");
pl("The color has been changed using a function that doesn't update acurattr,\n so aforcecolor can be used to resolve\n");
aforcecolor(ATTR(WHITE,CYAN));
pl("aforcecolor(ATTR(WHITE,CYAN))\n");
/* MISCELLANEOUS FUNCTIONS */
acolor(7);
aclrscr();
pl("MISCELLANEOUS FUNCTIONS:\n");
printf(
"Multiple test methods that test for ANSI presence -\n"
"atest()\n"
" aresult(): "
);
atest();
for (i=0;kbhit();i++) // read input buffer
s[i]=getch();
if (aresult(s))
printf("TRUE\n");
else
printf("FALSE\n");
printf(
"atest1()\n"
" aresult1(): "
);
atest1();
if (aresult1())
printf("TRUE\n");
else
printf("FALSE\n");
printf("aresult2(): ");
if (aresult2())
printf("TRUE\n");
else
printf("FALSE\n");
atest();
for (i=0;kbhit();i++) // read input buffer
s[i]=getch();
i=awherexy(s);
printf("awherexy(): Cursor at X:%d Y:%d at beginning of this line\n\n", i/256, i%256);
pl("akeyboard() will allow you to redefine keyboard or create macros if your ANSI\n emulator supports them.\n");
pl("asetmode() and aresetmode() changes screen modes and variable options. Watch.\n\nasetmode(CO40)");
asetmode(C40);
pl("now back to asetmode(C80)");
asetmode(C80);
pl("\n\n\n\nanocursorwrap() - turns off cursor wrap\n");
anocursorwrap();
// These printf strings exceed 80 columns.
printf("01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n");
pl("acursorwrap() - turns it back on.. Below is the same string as it should have\n appeared.\n");
acursorwrap();
printf("01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n");
pl("That concludes DEMOANSI tests.. ");
return;
}
results(int testnum) {
aforcegotoxy(57, 1);
aforcecolor(7);
pl("Press a key for results");
aclrscr();
printf("TEST %d\n", testnum);
printf("\nOPTIMIZED ANSI STATISTICS (OPTANSI)\n");
printf(
"Total number of characters counted in 100 repetitions of ANSI output: %5d\n"
"At 2400 baud that would equate to %2.2f seconds.\n"
"At 9600 baud that would equate to %2.2f seconds.\n"
"Time of operations on this computer was %2.4f seconds.\n",
ocnt, ocnt/240.0, ocnt/960.0, (float) otime/CLK_TCK);
printf("\nNON-OPTIMIZED ANSI STATISTICS (OTHER LIBRARIES)\n");
printf(
"Total number of characters counted in 100 repetitions of ANSI output: %5d\n"
"At 2400 baud that would equate to %2.2f seconds.\n"
"At 9600 baud that would equate to %2.2f seconds.\n"
"Time of operations on this computer was %2.4f seconds.\n",
nocnt, nocnt/240.0, nocnt/960.0, (float) notime/CLK_TCK);
printf("\nRELATIONAL IMPROVEMENT WHEN USING OPTANSI\n");
printf(
"Percent of output reduced using OptANSI library: %2.2f%%\n"
"At 2400 baud that would reduce time by %2.2f seconds.\n"
"At 9600 baud that would reduce time by %2.2f seconds.\n"
"Time saved on this computer was %2.4f seconds, %2.2f%%.\n",
(float)(nocnt-ocnt)/nocnt*100, (nocnt-ocnt)/240.0, (nocnt-ocnt)/960.0,
(float) (notime-otime)/CLK_TCK, (float) (notime-otime)/notime*100);
printf(
"\n"
"\n"
"Note: The DEMO version of the library has been slightly crippled to perform less"
"efficiently than the registered version."
);
return;
}
main() {
ainit();
ashort(aBKSP | aCR | !aLF | !aCLRSCR | aRESET | aNANSI);
atest1();
if (!(aresult1() || aresult2())) {
printf(
"DEMOANSI does not detect an ANSI device driver. Make sure to place\n"
"DEVICE=ANSI.SYS or equivalent in your CONFIG.SYS and reboot your machine\n"
"If you would like to continue this test without ANSI, press ENTER.\n"
);
if (getch()!=13)
return;
}
aforcecolor(31);
aclrscr();
aright(70);
printf("ESC Aborts");
agotoxy(1, 4);
printf(
"DEMOANSI: Welcome to the OptANSI library demo:\n"
"\n"
"ANSI, as you probably already know, is a useful emulation tool which adds\n"
"color, cursor location, and other functions to the otherwise standard\n"
"unidirectional TTY interface. It does this by sending a special set of\n"
"codes to the system for interpretting. ANSI has established its domain with\n"
"telecommunications and device drivers. ANSI codes tend to be very blocky and\n"
"in most circumstances are large strings for even simple commands. For instance,\n"
"to change a simple color, the ANSI string might be ESC[0;37;44;1;5m. Often\n"
"times all this data is not necessary. That's where OptANSI comes it. It takes\n"
"advantage of the fact that clock cycles on the computer are faster than the\n"
"communications interface and can find the shortest path, reducing the ANSI\n"
"string.\n"
"\n"
"Programmers should note that all the commands are highly optimized so that the\n"
"entire OptANSI code and data fits well under 1.5K. And all commands are very\n"
"similar to conio functions of the same name.\n"
"\n"
);
pl("**press a key**");
/* TEST 1 */
aclrscr();
adown(6);
printf(
"TEST 1\n"
"\n"
"On this next screen there will be a test of some of the most common ANSI\n"
"output codes. First the system will show you all the commands it is going to\n"
"send to the screen. Press a key after each of these. Then it will repeat these\n"
"same commands 100 times to calculate the output returned from OptANSI functions."
"Then it will repeat the same test using nonoptimized ANSI functions so you can\n"
"see the difference. You will find some bias in the results though. We have used\n"
"some of the cursor location functions that we know will optimize well. But this\n"
"is only to assure you of the extreme that OptANSI can be useful.\n"
"\n"
);
pl("**press a key**");
test1();
results(1);
pl("");
/* TEST 2 */
aforcecolor(31);
aclrscr();
adown(7);
printf(
"TEST 2\n"
"\n"
"On this next screen there will be a test similar to the last test except that\n"
"instead of best case scenario output, completely random screen and color data\n"
"will be sent to the ANSI output functions. This should severely reduce the\n"
"results OptANSI offers as compared with the last performance test, but you\n"
"should notice that OptANSI still offers significant improvements. Running this\n"
"second test will give varied results each time but only to a limited degree.\n"
"\n"
);
pl("**press a key**");
if (test2()) {
results(2);
getch();
}
/* TEST 3 */
aforcecolor(31);
aclrscr();
adown(8);
printf(
"TEST 3\n"
"\n"
"This last test is designed to test the majority of functions available in the\n"
"OptANSI library. It should give you a feel for the easiness of its use and\n"
"ensure that they work properly. After each command, press a key. Pay careful\n"
"attention to the current colors and current cursor locations just before\n"
"pressing a key.\n"
"\n"
);
pl("**press a key**");
acolor(7);
test3();
return;
}
/* These functions were ported from other standard ANSI output libraries */
int oldgotoxy(int x, int y) {
return(printf("\x1b[%d;%dH", y, x));
}
EXTERN char acolors[];
int oldcolor(int c) {
char str[25];
int f, b;
f=c & 7;
b=(c & 112) >> 4;
sprintf(str, "\x1b[0;3%c;4%c", acolors[f], acolors[b]);
if (c & 8)
strcat(str, ";1");
if (c & 128)
strcat(str, ";5");
strcat(str, "m");
return(printf(str));
}
int oldclrscr() {
return(printf("\x1b[2J"));
}